package com.example.myapp;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.File;

/**
 * Created by 艳双 on 2015/12/21.
 * project:TakePhoto
 * user:lisa[FR]
 * Date:2015/12/21
 */
public class CameraUtils {
    private static String filePath = null;

    /**
     * 裁剪
     * @param act
     * @param requestCode
     * @param sourceUri
     * @param targetUri
     */
    public static void goClipping(Activity act, int requestCode, Uri sourceUri, Uri targetUri){
        if(act == null || sourceUri == null || targetUri == null || requestCode <= 0) return;
        //对于拍照的照片进行裁剪
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(sourceUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 80);
        intent.putExtra("outputY", 80);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

        act.startActivityForResult(intent, requestCode);
    }
    //调用安卓自带的照相机进行拍照
    public static void goTakingPhotos(Activity act, int requestCode, Uri uri){
        goCamera(act, MediaStore.ACTION_IMAGE_CAPTURE, requestCode, uri);
    }
    //录像
    public static void goRecordingVideos(Activity act, int requestCode, Uri uri){
//		Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//		intent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, 768000);
//		intent.putExtra( android.provider.MediaStore.EXTRA_DURATION_LIMIT, 45000);
//		act.startActivityForResult(intent, requestCode);


        Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        //intent.putExtra(android.provider.MediaStore.EXTRA_SIZE_LIMIT, 768000);
        //intent.putExtra( android.provider.MediaStore.EXTRA_DURATION_LIMIT, 45000);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.1);
        act.startActivityForResult(intent, requestCode);

    }
    //相片存放的路径
    public static String takePhoto(final Activity activity, final String dir, final String filename, final int cmd) {
        filePath = dir + filename;

        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        final File cameraDir = new File(dir);
        if (!cameraDir.exists()) {
            System.out.println("??????????????????");
            return "";
        }

        final File file = new File(filePath);
        final Uri outputFileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        try {
            activity.startActivityForResult(intent, cmd);
        } catch (final ActivityNotFoundException e) {
            return "";
        }
        return filePath;
    }

    private static void goCamera(Activity act, String mediaType, int requestCode, Uri uri){
        //check
        if(act == null || requestCode <= 0 || uri == null) return;


        Intent intent=new Intent(mediaType);
        //自定义裁剪输出位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,3234567);
        act.startActivityForResult(intent, requestCode);
    }

    /**
     * 照相
     * @param activity
     * @param dir
     * @param filename
     * @param cmd
     * @return
     */
    public static String takePhotoTwo(final Activity activity, final String dir, final String filename, final int cmd) {
//		File file = new File(path);
        //判断文件夹是否存在,如果不存在则创建文件夹
//		if (!file.exists()) {
//			file.mkdir();
//		}
        filePath = dir + filename;

        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        final File cameraDir = new File(dir);
        if (!cameraDir.exists()) {
            System.out.println("文件夹不存在");
            return "";
        }

//		final File file = new File(filePath);
        final File filePathFile = new File(filePath);
        final Uri outputFileUri = Uri.fromFile(filePathFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        try {
            activity.startActivityForResult(intent, cmd);
        } catch (final ActivityNotFoundException e) {
            System.out.println("-----------》"+e.getMessage());
            return "";
        }
        return filePath;
    }

}
